package od_2025A.one;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * https://blog.csdn.net/qq_45776114/article/details/145684520
 * 示例：
 * 输入：
 * 5,8,11,3,6,8,8,-1,11,2,11,11
 * 11,2,11,8,6,8,8,-1,8,15,3,-9,11
 * 输出：
 * 1:-1,2,3,6
 * 3:8,11
 * 总结：这道题的思路，我和答案是一样的
 * 1、答案中用道 TreeMap 和 TreeSet 来进行排序
 * 2、我的比较器还是写的比较垃圾哎，时不时就不会写了
 * 3、关于 map.computeIfAbsent 的用法
 */
public class Test07 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] first = scanner.nextLine().split(",");
        String[] second = scanner.nextLine().split(",");
        scanner.close();
        // 解析input为数组
        List<Integer> firstList = parse(first);
        List<Integer> secondList = parse(second);
        // 将数组转换为 map
        Map<Integer, Integer> firstMap = convert2Map(firstList);
        Map<Integer, Integer> secondMap = convert2Map(secondList);
        // 收集重复的元素及重复的次数
        Map<Integer, TreeSet<Integer>> result = new TreeMap<>();
        firstMap.forEach((element, firstCount) -> {
            Integer secondCount = secondMap.get(element);
            // 没有重复，则 continue
            if (Objects.isNull(secondCount)) {
                return;
            }
            // 获取重复次数
            int repeatCount = Math.min(firstCount, secondCount);
            // 将重复的元素添加道 treeSet中
            result.computeIfAbsent(repeatCount, key -> new TreeSet<>()).add(element);
        });
        // 打印结果
        if (result.isEmpty()) {
            System.out.println("NULL");
        } else {
            for (Map.Entry<Integer, TreeSet<Integer>> entry : result.entrySet()) {
                String[] strArray = entry.getValue().stream().map(String::valueOf).toArray(String[]::new);
                String line = entry.getKey() + ":" + String.join(",", strArray);
                System.out.println(line);
            }
        }
    }

    /**
     * element -> count
     *
     * @param list
     * @return
     */
    private static Map<Integer, Integer> convert2Map(List<Integer> list) {
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer element : list) {
            map.put(element, map.getOrDefault(element, 0) + 1);
        }
        return map;
    }

    /**
     * 将input内容解析为数组
     *
     * @param input
     * @return
     */
    private static List<Integer> parse(String[] input) {
        return Stream.of(input).map(Integer::parseInt).collect(Collectors.toList());
    }


}
